package com.ly.web.controller.erp;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ly.common.annotation.Log;
import com.ly.common.core.controller.BaseController;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.enums.BusinessType;
import com.ly.heart.domain.CustomerAttributeEntity;
import com.ly.heart.service.ICustomerAttributeService;
import com.ly.common.utils.poi.ExcelUtil;
import com.ly.common.core.page.TableDataInfo;

/**
 * 客户属性Controller
 * @author cxl
 * @date 2024-05-14
 */
@Api (value = "CustomerAttribute", tags = "客户属性/项目中的单位性质")
@RestController
@RequestMapping ("/heart/CustomerAttribute")
public class CustomerAttributeController extends BaseController {
    @Resource
    private ICustomerAttributeService customerAttributeEntityService;

    /**
     * 查询客户属性列表
     */
    @PreAuthorize ("@ss.hasPermi('heart:CustomerAttribute:list')")
    @GetMapping ("/list")
    @ApiOperation ("查询客户属性列表")
    public TableDataInfo list (CustomerAttributeEntity customerAttributeEntity) {
        startPage ();
        List<CustomerAttributeEntity> list = customerAttributeEntityService.selectCustomerAttributeEntityList ( customerAttributeEntity );
        return getDataTable ( list );
    }

    /**
     * 导出客户属性列表
     */
    @PreAuthorize ("@ss.hasPermi('heart:CustomerAttribute:export')")
    @Log (title = "客户属性", businessType = BusinessType.EXPORT)
    @PostMapping ("/export")
    @ApiOperation ("导出客户属性列表")
    public void export (HttpServletResponse response, CustomerAttributeEntity customerAttributeEntity) {
        List<CustomerAttributeEntity> list = customerAttributeEntityService.selectCustomerAttributeEntityList ( customerAttributeEntity );
        ExcelUtil<CustomerAttributeEntity> util = new ExcelUtil<CustomerAttributeEntity> ( CustomerAttributeEntity.class );
        util.exportExcel ( list, "客户属性数据" );
    }

    /**
     * 获取客户属性详细信息
     */
    @PreAuthorize ("@ss.hasPermi('heart:CustomerAttribute:query')")
    @GetMapping (value = "/{id}")
    @ApiOperation ("获取客户属性详细信息")
    public AjaxResult getInfo (@PathVariable ("id") String id) {
        return AjaxResult.success ( customerAttributeEntityService.selectCustomerAttributeEntityById ( id ) );
    }

    /**
     * 新增客户属性
     */
//    @PreAuthorize("@ss.hasPermi('heart:CustomerAttribute:add')")
    @Log (title = "客户属性", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation ("新增客户属性")
    public AjaxResult add (@RequestBody CustomerAttributeEntity customerAttributeEntity) {
        return toAjax ( customerAttributeEntityService.insertCustomerAttributeEntity ( customerAttributeEntity ) );
    }

    /**
     * 修改客户属性
     */
    @PreAuthorize ("@ss.hasPermi('heart:CustomerAttribute:edit')")
    @Log (title = "客户属性", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation ("修改客户属性")
    public AjaxResult edit (@RequestBody CustomerAttributeEntity customerAttributeEntity) {
        return toAjax ( customerAttributeEntityService.updateCustomerAttributeEntity ( customerAttributeEntity ) );
    }

    /**
     * 删除客户属性
     */
    @PreAuthorize ("@ss.hasPermi('heart:CustomerAttribute:remove')")
    @Log (title = "客户属性", businessType = BusinessType.DELETE)
    @DeleteMapping ("/{ids}")
    @ApiOperation ("删除客户属性")
    public AjaxResult remove (@PathVariable String[] ids) {
        return toAjax ( customerAttributeEntityService.deleteCustomerAttributeEntityByIds ( ids ) );
    }

    @PostMapping ("/getNoRepeatCustomerAttribute")
    @ApiOperation ("根据名称判重接口")
    public AjaxResult getNoRepeatCustomerAttribute (@RequestBody CustomerAttributeEntity customerAttributeEntity) {
        CustomerAttributeEntity one = customerAttributeEntityService.getOne ( new LambdaQueryWrapper<CustomerAttributeEntity>().eq ( CustomerAttributeEntity :: getAttrName, customerAttributeEntity.getAttrName () ) );
        if ( ObjectUtil.isNotEmpty ( one ) ) {
            return AjaxResult.error ( "该名称已存在" );
        }
        return AjaxResult.success ();
    }
}
